app.service('agentAreaService',['$http','$q','utilService',function($http,$q,utilService){

    this.add = function($scope){
        var districtIds = '';
        for(var id in $scope.agentArea.districtIds) {
            var isChecked = $scope.agentArea.districtIds[id];
            if(isChecked) {
                districtIds += id + ',';
            }
        }

        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domainAgent + 'manage/agentArea/save',
            data:{
                name: $scope.agentArea.name,
                areaIds: districtIds
            },
            method:'POST'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    this.delete = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domainAgent + 'manage/agentArea/'+ $scope.id,
            method:"DELETE"
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    this.update = function($scope){
        var districtIds = '';
        for(var id in $scope.district.checkEle) {
            var isChecked = $scope.district.checkEle[id];
            if(isChecked) {
                districtIds += id + ',';
            }
        }

        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domainAgent + 'manage/agentArea/save',
            data:{
                id: $scope.editData.id,
                name: $scope.editData.name,
                level: $scope.editData.level,
                areaIds: districtIds
            },
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    this.find = function($scope, id){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domainAgent + 'manage/agentArea/'+ id,
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }
}]);